package com.miniapp.talk.activity.family;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.http.imageloader.glide.ImageConfigImpl;
import com.jess.arms.utils.ArmsUtils;
import com.miniapp.talk.R;
import com.miniapp.talk.adapter.family.FamilyDetailsAdapter;
import com.miniapp.talk.app.utils.RxUtils;
import com.miniapp.talk.base.MyBaseArmActivity;
import com.miniapp.talk.base.UserManager;
import com.miniapp.talk.bean.CommentBean;
import com.miniapp.talk.bean.FamilyUser;
import com.miniapp.talk.bean.FirstEvent;
import com.miniapp.talk.bean.GetFamilyDetailResult;
import com.miniapp.talk.di.CommonModule;
import com.miniapp.talk.di.DaggerCommonComponent;
import com.miniapp.talk.service.CommonModel;
import com.qmuiteam.qmui.widget.QMUIRadiusImageView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

import static com.miniapp.talk.utils.Constant.FAMILYXIUGAI;

/**
 * 家族详情
 * 老王
 */

public class FamilyDetailsActivity extends MyBaseArmActivity {
    @Inject
    CommonModel commonModel;
    @BindView(R.id.img_family_head)
    QMUIRadiusImageView imgFamilyHead;
    @BindView(R.id.family_time)
    TextView familyTime;
    @BindView(R.id.tv_name_count)
    TextView tvNameCount;
    @BindView(R.id.tv_intro_count)
    TextView tvIntroCount;
    @BindView(R.id.tv_notice_count)
    TextView tvNoticeCount;
    @BindView(R.id.family_member)
    TextView familyMember;
    @BindView(R.id.family_recyc)
    RecyclerView familyRecyc;
    @BindView(R.id.jiaru_btn)
    ImageView jiaruBtn;
    @BindView(R.id.toolbar_back)
    RelativeLayout toolbarBack;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.img_bar_right)
    ImageView imgBarRight;
    @BindView(R.id.toolbar_right)
    RelativeLayout toolbarRight;
    @BindView(R.id.toolbar)
    Toolbar toolbar;

    private String family_id;
    private int itemCount;
    private FamilyDetailsAdapter mAdapter;
    private List<FamilyUser> familyUserList;
    private GetFamilyDetailResult mBean;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerCommonComponent.builder()
                .appComponent(appComponent)
                .commonModule(new CommonModule())
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_family_details;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        setTitle("家族详情");

        family_id = getIntent().getStringExtra("family_id");

        int screenWidth = ScreenUtils.getScreenWidth();
        itemCount = screenWidth / SizeUtils.dp2px(50);
        mAdapter = new FamilyDetailsAdapter(this);
        familyRecyc.setLayoutManager(new GridLayoutManager(this, itemCount));
        familyRecyc.setAdapter(mAdapter);

        getFamilyDetailsData();
    }

    //获取家族详情的数据
    private void getFamilyDetailsData() {
        RxUtils.loading(commonModel.getFamilyDetail(family_id), this)
                .subscribe(new ErrorHandleSubscriber<GetFamilyDetailResult>(mErrorHandler) {
                    @Override
                    public void onNext(GetFamilyDetailResult getFamilyDetailResult) {
                        setFamilyData(getFamilyDetailResult);

//                        List<FamilyUser> userList= getFamilyDetailResult.getData().getFamily_user_list();
//
//                        if(userList != null && userList.size()>0){
//                            String userId = UserManager.getUser().getUserId()+"";
//                            for(FamilyUser familyUser:userList){
//                                if(userId.equals(familyUser.getUser_id())){//我在此房间
//                                    mMeInFamily = true;
//                                    break;
//                                }
//                            }
//                        }
                    }
                });
    }


    //设置家族数据
    private void setFamilyData(GetFamilyDetailResult getFamilyDetailResult) {
        mBean = getFamilyDetailResult;

        //家族的头像
        ArmsUtils.obtainAppComponentFromContext(FamilyDetailsActivity.this)
                .imageLoader()
                .loadImage(FamilyDetailsActivity.this, ImageConfigImpl
                        .builder()
                        .url(getFamilyDetailResult.getData().getImage())
                        .placeholder(R.mipmap.no_tou)
                        .imageView(imgFamilyHead)
                        .errorPic(R.mipmap.no_tou)
                        .build());

        //家族创建成功的时间
        if (TextUtils.isEmpty(getFamilyDetailResult.getData().getSuctime())) {
            familyTime.setVisibility(View.GONE);
        } else {
            familyTime.setVisibility(View.VISIBLE);
            familyTime.setText(getFamilyDetailResult.getData().getSuctime());
        }

        //家族名称
        tvNameCount.setText(getFamilyDetailResult.getData().getName());

        //家族介绍
        if (TextUtils.isEmpty(getFamilyDetailResult.getData().getIntroduce())) {
//            tvIntroCount.setVisibility(View.GONE);
            tvIntroCount.setText("暂无");
        } else {
//            tvIntroCount.setVisibility(View.VISIBLE);
            tvIntroCount.setText(getFamilyDetailResult.getData().getIntroduce());
        }

        //家族公告
        if (TextUtils.isEmpty(getFamilyDetailResult.getData().getNotice())) {
//            tvNoticeCount.setVisibility(View.GONE);
            tvNoticeCount.setText("暂无");
        } else {
//            tvNoticeCount.setVisibility(View.VISIBLE);
            tvNoticeCount.setText(getFamilyDetailResult.getData().getNotice());
        }

        //家族人数
        familyMember.setText(getFamilyDetailResult.getData().getNum() + "人");

        //申请加入的按钮显示与否
        if (getFamilyDetailResult.getData().is_show() == 0) {
            jiaruBtn.setVisibility(View.GONE);
        } else {
            jiaruBtn.setVisibility(View.VISIBLE);
        }
        if (getFamilyDetailResult.getData().is_edit() == 0) {
            toolbarRight.setVisibility(View.GONE);
        } else {
            toolbarRight.setVisibility(View.VISIBLE);
        }


        familyUserList = new ArrayList<>();
        List<FamilyUser> family_user_list = getFamilyDetailResult.getData().getFamily_user_list();
        for (int a = 0; a < family_user_list.size(); a++) {
            if ((itemCount - 1) >= a) {
                familyUserList.add(family_user_list.get(a));
            }
        }
        mAdapter.setNewData(familyUserList);
    }

    //申请加入家族
    private void actionApplyFamily() {
        RxUtils.loading(commonModel.actionApplyFamily(family_id), this)
                .subscribe(new ErrorHandleSubscriber<CommentBean>(mErrorHandler) {
                    @Override
                    public void onNext(CommentBean commentBean) {
                        if (commentBean.getCode() == 1) {
                            finish();
                        }
                    }
                });
    }

    @OnClick({R.id.jiaru_btn, R.id.toolbar_right,R.id.layout_users})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.jiaru_btn:
                actionApplyFamily();
                break;
            case R.id.toolbar_right:
                Intent intent = new Intent(FamilyDetailsActivity.this, CreateFamilyModifyActivity.class);
                intent.putExtra("family_id", family_id);
                intent.putExtra("user_type", mBean.getData().getUser_type());
                intent.putExtra("head_url", mBean.getData().getImage());
                intent.putExtra("family_name", mBean.getData().getName());
                intent.putExtra("family_gg", mBean.getData().getNotice());
                intent.putExtra("family_js", mBean.getData().getIntroduce());
                startActivity(intent);
                break;
            case R.id.layout_users://家族成员

                if(mBean == null){
                    return;
                }

                Intent intent1 = new  Intent(this, FamilyUserListActivity.class);

                intent1.putExtra("family_id",family_id);

                intent1.putExtra("family_name",mBean.getData().getName());

                intent1.putExtra("user_type",Integer.parseInt(mBean.getData().getUser_type()));

                startActivity(intent1);

                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void receiveMsg(FirstEvent event) {
        String tag = event.getTag();
        if (FAMILYXIUGAI.equals(tag)) {
            getFamilyDetailsData();
        }
    }

}
